//
//  SRSystemService.m
//  SportRing
//
//  Created by Apple on 14/10/29.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSystemNetService.h"
#import "SRSystemNetSource.h"
#import "SRDB.h"

@implementation SRSystemNetService

+ (RACSignal *)getPoster
{
    RACSubject * subject = [RACSubject subject];
    [SRSystemNetSource getPosterWithSuccess:^(NSDictionary *dict)
     {
         if ([[dict objectForKey:@"code"] intValue] == 0)
         {
             SRSystem * system = [SRSystem findDefaultSystem];
             [system configAttributesWithBlock:^(SRSystem *aSelf) {
                 aSelf.posterUrl = dict[@"data"][@"poster"];
             }];
             [subject sendNext:system];
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }

     }
                                           failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//吐槽上传
+ (RACSignal *)careCommonWithThink:(NSString *)think
{
    RACSubject * subject = [RACSubject subject];
    SRMe * me = [SRMe currentUser];
    [SRSystemNetSource careCommonWithToken:me.token
                                    IThink:think
                                   success:^(NSDictionary *dict)
     {
         if ([[dict objectForKey:@"code"] intValue] == 0)
         {
             [subject sendNext:nil];

         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }
     }
                                    failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//获取里程碑信息

+ (RACSignal *)getMilepost
{
    RACSubject * subject = [RACSubject subject];
    [SRSystemNetSource getMilepostWithSuccess:^(NSDictionary *dict)
     {
         if ([[dict objectForKey:@"code"] intValue] == 0)
         {
             SRSystem * system = [SRSystem findDefaultSystem];
             [system configAttributesWithBlock:^(SRSystem *aSelf) {
                 aSelf.userCount = @([dict[@"data"][@"account"] intValue]);
                 aSelf.mileage   = @([dict[@"data"][@"mileage"] intValue]);
                 aSelf.calorie   = @([dict[@"data"][@"calorie"] intValue]);
             }];
             [subject sendNext:system];
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }

     }
                                        failure:^(NSError *error)
     {
         [subject sendError:error];
     }];

    return subject;
}

+ (RACSignal *)getMilepostWithSuccess:(CompletionBlockWithDictionary)success fail:(CompletionBlockWithError)fail
{
    RACSubject * subject = [RACSubject subject];
    [SRSystemNetSource getMilepostWithSuccess:^(NSDictionary *dict)
     {
         if ([[dict objectForKey:@"code"] intValue] == 0)
         {
             SRSystem * system = [SRSystem findDefaultSystem];
             [system configAttributesWithBlock:^(SRSystem *aSelf) {
                 aSelf.userCount = @([dict[@"data"][@"account"] intValue]);
                 aSelf.mileage   = @([dict[@"data"][@"mileage"] intValue]);
                 aSelf.calorie   = @([dict[@"data"][@"calorie"] intValue]);
             }];
             [subject sendNext:system];
             success(dict);
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }
         
     }
                                      failure:^(NSError *error)
     {
         [subject sendError:error];
         fail(error);
     }];
    
    return subject;
}

+ (RACSignal *)getUserMilepostWithSuccess:(CompletionBlockWithDictionary)success fail:(CompletionBlockWithError)fail {
    
    RACSubject * subject = [RACSubject subject];
    [SRSystemNetSource postUserMilepostWithToken:[SRMe currentUser].token success:^(NSDictionary *dict) {
        
        if ([[dict objectForKey:@"code"] intValue] == 0)
        {
            SRSystem * system = [SRSystem findDefaultSystem];
            [system configAttributesWithBlock:^(SRSystem *aSelf) {
                aSelf.userCount = @([dict[@"data"][@"account"] intValue]);
                aSelf.mileage   = @([dict[@"data"][@"mileage"] intValue]);
                aSelf.calorie   = @([dict[@"data"][@"calorie"] intValue]);
            }];
            [subject sendNext:system];
            success(dict);
        }
        else
        {
            [subject sendError:[NSError errorWithDomain:@"error"
                                                   code:[dict[@"code"] intValue]
                                               userInfo:@{@"tip":dict[@"tip"]}]];
        }
    } failure:^(NSError *error) {
        [subject sendError:error];
        fail(error);
    }];
//    [SRSystemNetSource getMilepostWithSuccess:^(NSDictionary *dict)
//     {
//         if ([[dict objectForKey:@"code"] intValue] == 0)
//         {
//             SRSystem * system = [SRSystem findDefaultSystem];
//             [system configAttributesWithBlock:^(SRSystem *aSelf) {
//                 aSelf.userCount = @([dict[@"data"][@"account"] intValue]);
//                 aSelf.mileage   = @([dict[@"data"][@"mileage"] intValue]);
//                 aSelf.calorie   = @([dict[@"data"][@"calorie"] intValue]);
//             }];
//             [subject sendNext:system];
//             success(dict);
//         }
//         else
//         {
//             [subject sendError:[NSError errorWithDomain:@"error"
//                                                    code:[dict[@"code"] intValue]
//                                                userInfo:@{@"tip":dict[@"tip"]}]];
//         }
//         
//     }
//                                      failure:^(NSError *error)
//     {
//         [subject sendError:error];
//         fail(error);
//     }];
    
    return subject;
}

//获取里程碑信息
+ (RACSignal *)checkVersionUpdate
{
    RACSubject * subject = [RACSubject subject];
    
    [SRSystemNetSource checkVersionUpdateWithSuccess:^(NSDictionary *dict) {
        [subject sendNext:dict];
    } failure:^(NSError *error) {
        [subject sendError:error];
    }];
    
    return subject;
}

@end
